package owePaybackLog

import (
	"github.com/gofiber/fiber/v2"
	"sg-backend/global"
	"sg-backend/model/paylog"
)

/*
 * method  GET
 * path    /api/oweLog/paybackLog
 */

// GetOwePaybackLog GET请求，获取数据
func GetOwePaybackLog(ctx *fiber.Ctx) error {

	// 计算分页，mysql第一页为0计算
	oweId := ctx.QueryInt("oweId")

	// 指定数据model
	var owePaybackLogs []paylog.OwePaybackLog

	// 匹配全部数据，计算总数
	r := global.DB.Where("owe_log_id = ?", oweId).
		Model(&paylog.OwePaybackLog{}).Order("time desc").Find(&owePaybackLogs)
	if r.Error != nil {
		return ctx.JSON(fiber.Map{
			"status":  "error",
			"message": "数据库查询出错！" + r.Error.Error(),
		})
	}

	// 查询结束返回结果
	return ctx.JSON(fiber.Map{
		"status":  "done",
		"success": true,
		"message": "请求成功！",
		"data":    owePaybackLogs,
	})
}
